package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yan
 * @date 2022/3/2
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;


    @PostMapping
    public Result<AddressBook> saveAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.saveAddressBook(addressBook);
    }


    @PutMapping("default")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        return addressBookService.setDefault(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> findAddressBookById(@PathVariable Long id){
        return addressBookService.findAddressBookById(id);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("default")
    public Result<AddressBook> getDefault(){
        return addressBookService.getDefault();
    }

    @GetMapping("/list")
    public Result<List<AddressBook>> findAddressBookList(AddressBook addressBook){
        return addressBookService.findAddressBookList(addressBook);

    }

    @PutMapping
    public Result<String> updateAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.updateAddressBook(addressBook);
    }

    @DeleteMapping
    public Result<String> deleteAddressBook(Long ids){
        return addressBookService.deleteAddressBook(ids);
    }

}
